import React, { Component } from 'react'
import moment from 'moment';

// let r = moment().format('YYYY-MM-DD HH:mm:ss'); // format 格式化
export default class Clock extends Component {
  state = {
    timeStr : moment().format('YYYY-MM-DD HH:mm:ss')
  }

  render() {
    return (
      <div style={{display: 'inline-block', border: 'solid 1px #258', padding: 20}}>
        {this.state.timeStr}
      </div>
    )
  }

  //组件挂载完成的回调
  componentDidMount(){
    //启动定时器
    this.timer = setInterval(() => {
      console.log(123);
      //更新状态
      this.setState({
        timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
      })
    }, 1000);
  }

  //组件卸载之前
  componentWillUnmount(){
    clearInterval(this.timer);
  }
}
